﻿$(document).ready(function () {
    function BindLogTable() {
        $("#LogTable").empty();
        $("#LogTable").after("<div class='loadingImage'></div>");

        var data = {
            "OrderBy": orderByColumn,
            "Desc": descanding
        };

        $.get(pathPrefix + "Admin/GetLogData", data, function (result) {
            if (result != null) {
                var TableHeaders = result.TableHeaders;
                var TableRows = result.TableRows;

                //add the header
                var headerRow = $("<tr>");
                for (var i = 0; i < TableHeaders.length; i++) {
                    headerRow.append("<th><a href=''>" + TableHeaders[i] + "</a></th>");
                }
                $("#LogTable").append(headerRow);

                //add the rows
                for (var i = 0; i < TableRows.length; i++) {
                    var row = $("<tr>");
                    for (var j = 0; j < TableRows[i].length; j++) {
                        row.append("<td>" + TableRows[i][j] + "</td>");
                    }
                    $("#LogTable").append(row);
                }

                tableAltRows();

                $("#errorMessage").hide();
            }
            else {
                $("#errorMessage").show();
            }

            $("#LogTable").siblings(".loadingImage").remove();
        }, "json");
    }

    var orderByColumn = "";
    var descanding = false;
    BindLogTable();

    $("#BindLogTable").click(function () {
        orderByColumn = "";
        descanding = false;
        BindLogTable();
    });

    //when clicking on a table header - OrderBy
    $("#LogTable th a").live("click", function () {
        //if the user select to order by the same column again
        if ($(this).html() == orderByColumn) {
            descanding = !descanding;
        }

        orderByColumn = $(this).html();

        BindLogTable();

        return false;
    });
});